<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>menubutton.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_menubutton.js.html">menubutton.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A menu button control.
<a name="line17"></a> *
<a name="line18"></a> * @author attila@google.com (Attila Bodis)
<a name="line19"></a> * @see ../demos/menubutton.html
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.ui.MenuButton&#39;);
<a name="line23"></a>
<a name="line24"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line25"></a>goog.require(&#39;goog.a11y.aria&#39;);
<a name="line26"></a>goog.require(&#39;goog.a11y.aria.State&#39;);
<a name="line27"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line28"></a>goog.require(&#39;goog.dom&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line30"></a>goog.require(&#39;goog.events.KeyCodes&#39;);
<a name="line31"></a>goog.require(&#39;goog.events.KeyHandler.EventType&#39;);
<a name="line32"></a>goog.require(&#39;goog.math.Box&#39;);
<a name="line33"></a>goog.require(&#39;goog.math.Rect&#39;);
<a name="line34"></a>goog.require(&#39;goog.positioning&#39;);
<a name="line35"></a>goog.require(&#39;goog.positioning.Corner&#39;);
<a name="line36"></a>goog.require(&#39;goog.positioning.MenuAnchoredPosition&#39;);
<a name="line37"></a>goog.require(&#39;goog.style&#39;);
<a name="line38"></a>goog.require(&#39;goog.ui.Button&#39;);
<a name="line39"></a>goog.require(&#39;goog.ui.Component.EventType&#39;);
<a name="line40"></a>goog.require(&#39;goog.ui.Component.State&#39;);
<a name="line41"></a>goog.require(&#39;goog.ui.Menu&#39;);
<a name="line42"></a>goog.require(&#39;goog.ui.MenuButtonRenderer&#39;);
<a name="line43"></a>goog.require(&#39;goog.ui.registry&#39;);
<a name="line44"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line45"></a>goog.require(&#39;goog.userAgent.product&#39;);
<a name="line46"></a>
<a name="line47"></a>
<a name="line48"></a>
<a name="line49"></a>/**
<a name="line50"></a> * A menu button control.  Extends {@link goog.ui.Button} by composing a button
<a name="line51"></a> * with a dropdown arrow and a popup menu.
<a name="line52"></a> *
<a name="line53"></a> * @param {goog.ui.ControlContent} content Text caption or existing DOM
<a name="line54"></a> *     structure to display as the button&#39;s caption (if any).
<a name="line55"></a> * @param {goog.ui.Menu=} opt_menu Menu to render under the button when clicked.
<a name="line56"></a> * @param {goog.ui.ButtonRenderer=} opt_renderer Renderer used to render or
<a name="line57"></a> *     decorate the menu button; defaults to {@link goog.ui.MenuButtonRenderer}.
<a name="line58"></a> * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM hepler, used for
<a name="line59"></a> *     document interaction.
<a name="line60"></a> * @constructor
<a name="line61"></a> * @extends {goog.ui.Button}
<a name="line62"></a> */
<a name="line63"></a>goog.ui.MenuButton = function(content, opt_menu, opt_renderer, opt_domHelper) {
<a name="line64"></a>  goog.ui.Button.call(this, content, opt_renderer ||
<a name="line65"></a>      goog.ui.MenuButtonRenderer.getInstance(), opt_domHelper);
<a name="line66"></a>
<a name="line67"></a>  // Menu buttons support the OPENED state.
<a name="line68"></a>  this.setSupportedState(goog.ui.Component.State.OPENED, true);
<a name="line69"></a>
<a name="line70"></a>  /**
<a name="line71"></a>   * The menu position on this button.
<a name="line72"></a>   * @type {!goog.positioning.AnchoredPosition}
<a name="line73"></a>   * @private
<a name="line74"></a>   */
<a name="line75"></a>  this.menuPosition_ = new goog.positioning.MenuAnchoredPosition(
<a name="line76"></a>      null, goog.positioning.Corner.BOTTOM_START);
<a name="line77"></a>
<a name="line78"></a>  if (opt_menu) {
<a name="line79"></a>    this.setMenu(opt_menu);
<a name="line80"></a>  }
<a name="line81"></a>  this.menuMargin_ = null;
<a name="line82"></a>  this.timer_ = new goog.Timer(500);  // 0.5 sec
<a name="line83"></a>
<a name="line84"></a>  // Phones running iOS prior to version 4.2.
<a name="line85"></a>  if ((goog.userAgent.product.IPHONE || goog.userAgent.product.IPAD) &amp;&amp;
<a name="line86"></a>      // Check the webkit version against the version for iOS 4.2.1.
<a name="line87"></a>      !goog.userAgent.isVersion(&#39;533.17.9&#39;)) {
<a name="line88"></a>    // @bug 4322060 This is required so that the menu works correctly on
<a name="line89"></a>    // iOS prior to version 4.2. Otherwise, the blur action closes the menu
<a name="line90"></a>    // before the menu button click can be processed.
<a name="line91"></a>    this.setFocusablePopupMenu(true);
<a name="line92"></a>  }
<a name="line93"></a>};
<a name="line94"></a>goog.inherits(goog.ui.MenuButton, goog.ui.Button);
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * The menu.
<a name="line99"></a> * @type {goog.ui.Menu|undefined}
<a name="line100"></a> * @private
<a name="line101"></a> */
<a name="line102"></a>goog.ui.MenuButton.prototype.menu_;
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * The position element.  If set, use positionElement_ to position the
<a name="line107"></a> * popup menu instead of the default which is to use the menu button element.
<a name="line108"></a> * @type {Element|undefined}
<a name="line109"></a> * @private
<a name="line110"></a> */
<a name="line111"></a>goog.ui.MenuButton.prototype.positionElement_;
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * The margin to apply to the menu&#39;s position when it is shown.  If null, no
<a name="line116"></a> * margin will be applied.
<a name="line117"></a> * @type {goog.math.Box}
<a name="line118"></a> * @private
<a name="line119"></a> */
<a name="line120"></a>goog.ui.MenuButton.prototype.menuMargin_;
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Whether the attached popup menu is focusable or not (defaults to false).
<a name="line125"></a> * Popup menus attached to menu buttons usually don&#39;t need to be focusable,
<a name="line126"></a> * i.e. the button retains keyboard focus, and forwards key events to the
<a name="line127"></a> * menu for processing.  However, menus like {@link goog.ui.FilteredMenu}
<a name="line128"></a> * need to be focusable.
<a name="line129"></a> * @type {boolean}
<a name="line130"></a> * @private
<a name="line131"></a> */
<a name="line132"></a>goog.ui.MenuButton.prototype.isFocusablePopupMenu_ = false;
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * A Timer to correct menu position.
<a name="line137"></a> * @type {goog.Timer}
<a name="line138"></a> * @private
<a name="line139"></a> */
<a name="line140"></a>goog.ui.MenuButton.prototype.timer_;
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/**
<a name="line144"></a> * The bounding rectangle of the button element.
<a name="line145"></a> * @type {goog.math.Rect}
<a name="line146"></a> * @private
<a name="line147"></a> */
<a name="line148"></a>goog.ui.MenuButton.prototype.buttonRect_;
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * The viewport rectangle.
<a name="line153"></a> * @type {goog.math.Box}
<a name="line154"></a> * @private
<a name="line155"></a> */
<a name="line156"></a>goog.ui.MenuButton.prototype.viewportBox_;
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * The original size.
<a name="line161"></a> * @type {goog.math.Size|undefined}
<a name="line162"></a> * @private
<a name="line163"></a> */
<a name="line164"></a>goog.ui.MenuButton.prototype.originalSize_;
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Do we render the drop down menu as a sibling to the label, or at the end
<a name="line169"></a> * of the current dom?
<a name="line170"></a> * @type {boolean}
<a name="line171"></a> * @private
<a name="line172"></a> */
<a name="line173"></a>goog.ui.MenuButton.prototype.renderMenuAsSibling_ = false;
<a name="line174"></a>
<a name="line175"></a>
<a name="line176"></a>/**
<a name="line177"></a> * Sets up event handlers specific to menu buttons.
<a name="line178"></a> * @override
<a name="line179"></a> */
<a name="line180"></a>goog.ui.MenuButton.prototype.enterDocument = function() {
<a name="line181"></a>  goog.ui.MenuButton.superClass_.enterDocument.call(this);
<a name="line182"></a>  if (this.menu_) {
<a name="line183"></a>    this.attachMenuEventListeners_(this.menu_, true);
<a name="line184"></a>  }
<a name="line185"></a>  var element = this.getElement();
<a name="line186"></a>  goog.asserts.assert(element, &#39;The menu button DOM element cannot be null.&#39;);
<a name="line187"></a>  goog.a11y.aria.setState(element, goog.a11y.aria.State.HASPOPUP, &#39;true&#39;);
<a name="line188"></a>};
<a name="line189"></a>
<a name="line190"></a>
<a name="line191"></a>/**
<a name="line192"></a> * Removes event handlers specific to menu buttons, and ensures that the
<a name="line193"></a> * attached menu also exits the document.
<a name="line194"></a> * @override
<a name="line195"></a> */
<a name="line196"></a>goog.ui.MenuButton.prototype.exitDocument = function() {
<a name="line197"></a>  goog.ui.MenuButton.superClass_.exitDocument.call(this);
<a name="line198"></a>  if (this.menu_) {
<a name="line199"></a>    this.setOpen(false);
<a name="line200"></a>    this.menu_.exitDocument();
<a name="line201"></a>    this.attachMenuEventListeners_(this.menu_, false);
<a name="line202"></a>
<a name="line203"></a>    var menuElement = this.menu_.getElement();
<a name="line204"></a>    if (menuElement) {
<a name="line205"></a>      goog.dom.removeNode(menuElement);
<a name="line206"></a>    }
<a name="line207"></a>  }
<a name="line208"></a>};
<a name="line209"></a>
<a name="line210"></a>
<a name="line211"></a>/** @override */
<a name="line212"></a>goog.ui.MenuButton.prototype.disposeInternal = function() {
<a name="line213"></a>  goog.ui.MenuButton.superClass_.disposeInternal.call(this);
<a name="line214"></a>  if (this.menu_) {
<a name="line215"></a>    this.menu_.dispose();
<a name="line216"></a>    delete this.menu_;
<a name="line217"></a>  }
<a name="line218"></a>  delete this.positionElement_;
<a name="line219"></a>  this.timer_.dispose();
<a name="line220"></a>};
<a name="line221"></a>
<a name="line222"></a>
<a name="line223"></a>/**
<a name="line224"></a> * Handles mousedown events.  Invokes the superclass implementation to dispatch
<a name="line225"></a> * an ACTIVATE event and activate the button.  Also toggles the visibility of
<a name="line226"></a> * the attached menu.
<a name="line227"></a> * @param {goog.events.Event} e Mouse event to handle.
<a name="line228"></a> * @override
<a name="line229"></a> * @protected
<a name="line230"></a> */
<a name="line231"></a>goog.ui.MenuButton.prototype.handleMouseDown = function(e) {
<a name="line232"></a>  goog.ui.MenuButton.superClass_.handleMouseDown.call(this, e);
<a name="line233"></a>  if (this.isActive()) {
<a name="line234"></a>    // The component was allowed to activate; toggle menu visibility.
<a name="line235"></a>    this.setOpen(!this.isOpen(), e);
<a name="line236"></a>    if (this.menu_) {
<a name="line237"></a>      this.menu_.setMouseButtonPressed(this.isOpen());
<a name="line238"></a>    }
<a name="line239"></a>  }
<a name="line240"></a>};
<a name="line241"></a>
<a name="line242"></a>
<a name="line243"></a>/**
<a name="line244"></a> * Handles mouseup events.  Invokes the superclass implementation to dispatch
<a name="line245"></a> * an ACTION event and deactivate the button.
<a name="line246"></a> * @param {goog.events.Event} e Mouse event to handle.
<a name="line247"></a> * @override
<a name="line248"></a> * @protected
<a name="line249"></a> */
<a name="line250"></a>goog.ui.MenuButton.prototype.handleMouseUp = function(e) {
<a name="line251"></a>  goog.ui.MenuButton.superClass_.handleMouseUp.call(this, e);
<a name="line252"></a>  if (this.menu_ &amp;&amp; !this.isActive()) {
<a name="line253"></a>    this.menu_.setMouseButtonPressed(false);
<a name="line254"></a>  }
<a name="line255"></a>};
<a name="line256"></a>
<a name="line257"></a>
<a name="line258"></a>/**
<a name="line259"></a> * Performs the appropriate action when the menu button is activated by the
<a name="line260"></a> * user.  Overrides the superclass implementation by not dispatching an {@code
<a name="line261"></a> * ACTION} event, because menu buttons exist only to reveal menus, not to
<a name="line262"></a> * perform actions themselves.  Calls {@link #setActive} to deactivate the
<a name="line263"></a> * button.
<a name="line264"></a> * @param {goog.events.Event} e Mouse or key event that triggered the action.
<a name="line265"></a> * @return {boolean} Whether the action was allowed to proceed.
<a name="line266"></a> * @override
<a name="line267"></a> * @protected
<a name="line268"></a> */
<a name="line269"></a>goog.ui.MenuButton.prototype.performActionInternal = function(e) {
<a name="line270"></a>  this.setActive(false);
<a name="line271"></a>  return true;
<a name="line272"></a>};
<a name="line273"></a>
<a name="line274"></a>
<a name="line275"></a>/**
<a name="line276"></a> * Handles mousedown events over the document.  If the mousedown happens over
<a name="line277"></a> * an element unrelated to the component, hides the menu.
<a name="line278"></a> * TODO(attila): Reconcile this with goog.ui.Popup (and handle frames/windows).
<a name="line279"></a> * @param {goog.events.BrowserEvent} e Mouse event to handle.
<a name="line280"></a> * @protected
<a name="line281"></a> */
<a name="line282"></a>goog.ui.MenuButton.prototype.handleDocumentMouseDown = function(e) {
<a name="line283"></a>  if (this.menu_ &amp;&amp;
<a name="line284"></a>      this.menu_.isVisible() &amp;&amp;
<a name="line285"></a>      !this.containsElement(/** @type {Element} */ (e.target))) {
<a name="line286"></a>    // User clicked somewhere else in the document while the menu was visible;
<a name="line287"></a>    // dismiss menu.
<a name="line288"></a>    this.setOpen(false);
<a name="line289"></a>  }
<a name="line290"></a>};
<a name="line291"></a>
<a name="line292"></a>
<a name="line293"></a>/**
<a name="line294"></a> * Returns true if the given element is to be considered part of the component,
<a name="line295"></a> * even if it isn&#39;t a DOM descendant of the component&#39;s root element.
<a name="line296"></a> * @param {Element} element Element to test (if any).
<a name="line297"></a> * @return {boolean} Whether the element is considered part of the component.
<a name="line298"></a> * @protected
<a name="line299"></a> */
<a name="line300"></a>goog.ui.MenuButton.prototype.containsElement = function(element) {
<a name="line301"></a>  return element &amp;&amp; goog.dom.contains(this.getElement(), element) ||
<a name="line302"></a>      this.menu_ &amp;&amp; this.menu_.containsElement(element) || false;
<a name="line303"></a>};
<a name="line304"></a>
<a name="line305"></a>
<a name="line306"></a>/** @override */
<a name="line307"></a>goog.ui.MenuButton.prototype.handleKeyEventInternal = function(e) {
<a name="line308"></a>  // Handle SPACE on keyup and all other keys on keypress.
<a name="line309"></a>  if (e.keyCode == goog.events.KeyCodes.SPACE) {
<a name="line310"></a>    // Prevent page scrolling in Chrome.
<a name="line311"></a>    e.preventDefault();
<a name="line312"></a>    if (e.type != goog.events.EventType.KEYUP) {
<a name="line313"></a>      // Ignore events because KeyCodes.SPACE is handled further down.
<a name="line314"></a>      return true;
<a name="line315"></a>    }
<a name="line316"></a>  } else if (e.type != goog.events.KeyHandler.EventType.KEY) {
<a name="line317"></a>    return false;
<a name="line318"></a>  }
<a name="line319"></a>
<a name="line320"></a>  if (this.menu_ &amp;&amp; this.menu_.isVisible()) {
<a name="line321"></a>    // Menu is open.
<a name="line322"></a>    var handledByMenu = this.menu_.handleKeyEvent(e);
<a name="line323"></a>    if (e.keyCode == goog.events.KeyCodes.ESC) {
<a name="line324"></a>      // Dismiss the menu.
<a name="line325"></a>      this.setOpen(false);
<a name="line326"></a>      return true;
<a name="line327"></a>    }
<a name="line328"></a>    return handledByMenu;
<a name="line329"></a>  }
<a name="line330"></a>
<a name="line331"></a>  if (e.keyCode == goog.events.KeyCodes.DOWN ||
<a name="line332"></a>      e.keyCode == goog.events.KeyCodes.UP ||
<a name="line333"></a>      e.keyCode == goog.events.KeyCodes.SPACE ||
<a name="line334"></a>      e.keyCode == goog.events.KeyCodes.ENTER) {
<a name="line335"></a>    // Menu is closed, and the user hit the down/up/space/enter key; open menu.
<a name="line336"></a>    this.setOpen(true);
<a name="line337"></a>    return true;
<a name="line338"></a>  }
<a name="line339"></a>
<a name="line340"></a>  // Key event wasn&#39;t handled by the component.
<a name="line341"></a>  return false;
<a name="line342"></a>};
<a name="line343"></a>
<a name="line344"></a>
<a name="line345"></a>/**
<a name="line346"></a> * Handles {@code ACTION} events dispatched by an activated menu item.
<a name="line347"></a> * @param {goog.events.Event} e Action event to handle.
<a name="line348"></a> * @protected
<a name="line349"></a> */
<a name="line350"></a>goog.ui.MenuButton.prototype.handleMenuAction = function(e) {
<a name="line351"></a>  // Close the menu on click.
<a name="line352"></a>  this.setOpen(false);
<a name="line353"></a>};
<a name="line354"></a>
<a name="line355"></a>
<a name="line356"></a>/**
<a name="line357"></a> * Handles {@code BLUR} events dispatched by the popup menu by closing it.
<a name="line358"></a> * Only registered if the menu is focusable.
<a name="line359"></a> * @param {goog.events.Event} e Blur event dispatched by a focusable menu.
<a name="line360"></a> */
<a name="line361"></a>goog.ui.MenuButton.prototype.handleMenuBlur = function(e) {
<a name="line362"></a>  // Close the menu when it reports that it lost focus, unless the button is
<a name="line363"></a>  // pressed (active).
<a name="line364"></a>  if (!this.isActive()) {
<a name="line365"></a>    this.setOpen(false);
<a name="line366"></a>  }
<a name="line367"></a>};
<a name="line368"></a>
<a name="line369"></a>
<a name="line370"></a>/**
<a name="line371"></a> * Handles blur events dispatched by the button&#39;s key event target when it
<a name="line372"></a> * loses keyboard focus by closing the popup menu (unless it is focusable).
<a name="line373"></a> * Only registered if the button is focusable.
<a name="line374"></a> * @param {goog.events.Event} e Blur event dispatched by the menu button.
<a name="line375"></a> * @override
<a name="line376"></a> * @protected
<a name="line377"></a> */
<a name="line378"></a>goog.ui.MenuButton.prototype.handleBlur = function(e) {
<a name="line379"></a>  if (!this.isFocusablePopupMenu()) {
<a name="line380"></a>    this.setOpen(false);
<a name="line381"></a>  }
<a name="line382"></a>  goog.ui.MenuButton.superClass_.handleBlur.call(this, e);
<a name="line383"></a>};
<a name="line384"></a>
<a name="line385"></a>
<a name="line386"></a>/**
<a name="line387"></a> * Returns the menu attached to the button.  If no menu is attached, creates a
<a name="line388"></a> * new empty menu.
<a name="line389"></a> * @return {goog.ui.Menu} Popup menu attached to the menu button.
<a name="line390"></a> */
<a name="line391"></a>goog.ui.MenuButton.prototype.getMenu = function() {
<a name="line392"></a>  if (!this.menu_) {
<a name="line393"></a>    this.setMenu(new goog.ui.Menu(this.getDomHelper()));
<a name="line394"></a>  }
<a name="line395"></a>  return this.menu_ || null;
<a name="line396"></a>};
<a name="line397"></a>
<a name="line398"></a>
<a name="line399"></a>/**
<a name="line400"></a> * Replaces the menu attached to the button with the argument, and returns the
<a name="line401"></a> * previous menu (if any).
<a name="line402"></a> * @param {goog.ui.Menu?} menu New menu to be attached to the menu button (null
<a name="line403"></a> *     to remove the menu).
<a name="line404"></a> * @return {goog.ui.Menu|undefined} Previous menu (undefined if none).
<a name="line405"></a> */
<a name="line406"></a>goog.ui.MenuButton.prototype.setMenu = function(menu) {
<a name="line407"></a>  var oldMenu = this.menu_;
<a name="line408"></a>
<a name="line409"></a>  // Do nothing unless the new menu is different from the current one.
<a name="line410"></a>  if (menu != oldMenu) {
<a name="line411"></a>    if (oldMenu) {
<a name="line412"></a>      this.setOpen(false);
<a name="line413"></a>      if (this.isInDocument()) {
<a name="line414"></a>        this.attachMenuEventListeners_(oldMenu, false);
<a name="line415"></a>      }
<a name="line416"></a>      delete this.menu_;
<a name="line417"></a>    }
<a name="line418"></a>    if (menu) {
<a name="line419"></a>      this.menu_ = menu;
<a name="line420"></a>      menu.setParent(this);
<a name="line421"></a>      menu.setVisible(false);
<a name="line422"></a>      menu.setAllowAutoFocus(this.isFocusablePopupMenu());
<a name="line423"></a>      if (this.isInDocument()) {
<a name="line424"></a>        this.attachMenuEventListeners_(menu, true);
<a name="line425"></a>      }
<a name="line426"></a>    }
<a name="line427"></a>  }
<a name="line428"></a>
<a name="line429"></a>  return oldMenu;
<a name="line430"></a>};
<a name="line431"></a>
<a name="line432"></a>
<a name="line433"></a>/**
<a name="line434"></a> * Specify which positioning algorithm to use.
<a name="line435"></a> *
<a name="line436"></a> * This method is preferred over the fine-grained positioning methods like
<a name="line437"></a> * setPositionElement, setAlignMenuToStart, and setScrollOnOverflow. Calling
<a name="line438"></a> * this method will override settings by those methods.
<a name="line439"></a> *
<a name="line440"></a> * @param {goog.positioning.AnchoredPosition} position The position of the
<a name="line441"></a> *     Menu the button. If the position has a null anchor, we will use the
<a name="line442"></a> *     menubutton element as the anchor.
<a name="line443"></a> */
<a name="line444"></a>goog.ui.MenuButton.prototype.setMenuPosition = function(position) {
<a name="line445"></a>  if (position) {
<a name="line446"></a>    this.menuPosition_ = position;
<a name="line447"></a>    this.positionElement_ = position.element;
<a name="line448"></a>  }
<a name="line449"></a>};
<a name="line450"></a>
<a name="line451"></a>
<a name="line452"></a>/**
<a name="line453"></a> * Sets an element for anchoring the menu.
<a name="line454"></a> * @param {Element} positionElement New element to use for
<a name="line455"></a> *     positioning the dropdown menu.  Null to use the default behavior
<a name="line456"></a> *     of positioning to this menu button.
<a name="line457"></a> */
<a name="line458"></a>goog.ui.MenuButton.prototype.setPositionElement = function(
<a name="line459"></a>    positionElement) {
<a name="line460"></a>  this.positionElement_ = positionElement;
<a name="line461"></a>  this.positionMenu();
<a name="line462"></a>};
<a name="line463"></a>
<a name="line464"></a>
<a name="line465"></a>/**
<a name="line466"></a> * Sets a margin that will be applied to the menu&#39;s position when it is shown.
<a name="line467"></a> * If null, no margin will be applied.
<a name="line468"></a> * @param {goog.math.Box} margin Margin to apply.
<a name="line469"></a> */
<a name="line470"></a>goog.ui.MenuButton.prototype.setMenuMargin = function(margin) {
<a name="line471"></a>  this.menuMargin_ = margin;
<a name="line472"></a>};
<a name="line473"></a>
<a name="line474"></a>
<a name="line475"></a>/**
<a name="line476"></a> * Adds a new menu item at the end of the menu.
<a name="line477"></a> * @param {goog.ui.MenuItem|goog.ui.MenuSeparator|goog.ui.Control} item Menu
<a name="line478"></a> *     item to add to the menu.
<a name="line479"></a> */
<a name="line480"></a>goog.ui.MenuButton.prototype.addItem = function(item) {
<a name="line481"></a>  this.getMenu().addChild(item, true);
<a name="line482"></a>};
<a name="line483"></a>
<a name="line484"></a>
<a name="line485"></a>/**
<a name="line486"></a> * Adds a new menu item at the specific index in the menu.
<a name="line487"></a> * @param {goog.ui.MenuItem|goog.ui.MenuSeparator} item Menu item to add to the
<a name="line488"></a> *     menu.
<a name="line489"></a> * @param {number} index Index at which to insert the menu item.
<a name="line490"></a> */
<a name="line491"></a>goog.ui.MenuButton.prototype.addItemAt = function(item, index) {
<a name="line492"></a>  this.getMenu().addChildAt(item, index, true);
<a name="line493"></a>};
<a name="line494"></a>
<a name="line495"></a>
<a name="line496"></a>/**
<a name="line497"></a> * Removes the item from the menu and disposes of it.
<a name="line498"></a> * @param {goog.ui.MenuItem|goog.ui.MenuSeparator} item The menu item to remove.
<a name="line499"></a> */
<a name="line500"></a>goog.ui.MenuButton.prototype.removeItem = function(item) {
<a name="line501"></a>  var child = this.getMenu().removeChild(item, true);
<a name="line502"></a>  if (child) {
<a name="line503"></a>    child.dispose();
<a name="line504"></a>  }
<a name="line505"></a>};
<a name="line506"></a>
<a name="line507"></a>
<a name="line508"></a>/**
<a name="line509"></a> * Removes the menu item at a given index in the menu and disposes of it.
<a name="line510"></a> * @param {number} index Index of item.
<a name="line511"></a> */
<a name="line512"></a>goog.ui.MenuButton.prototype.removeItemAt = function(index) {
<a name="line513"></a>  var child = this.getMenu().removeChildAt(index, true);
<a name="line514"></a>  if (child) {
<a name="line515"></a>    child.dispose();
<a name="line516"></a>  }
<a name="line517"></a>};
<a name="line518"></a>
<a name="line519"></a>
<a name="line520"></a>/**
<a name="line521"></a> * Returns the menu item at a given index.
<a name="line522"></a> * @param {number} index Index of menu item.
<a name="line523"></a> * @return {goog.ui.MenuItem?} Menu item (null if not found).
<a name="line524"></a> */
<a name="line525"></a>goog.ui.MenuButton.prototype.getItemAt = function(index) {
<a name="line526"></a>  return this.menu_ ?
<a name="line527"></a>      /** @type {goog.ui.MenuItem} */ (this.menu_.getChildAt(index)) : null;
<a name="line528"></a>};
<a name="line529"></a>
<a name="line530"></a>
<a name="line531"></a>/**
<a name="line532"></a> * Returns the number of items in the menu (including separators).
<a name="line533"></a> * @return {number} The number of items in the menu.
<a name="line534"></a> */
<a name="line535"></a>goog.ui.MenuButton.prototype.getItemCount = function() {
<a name="line536"></a>  return this.menu_ ? this.menu_.getChildCount() : 0;
<a name="line537"></a>};
<a name="line538"></a>
<a name="line539"></a>
<a name="line540"></a>/**
<a name="line541"></a> * Shows/hides the menu button based on the value of the argument.  Also hides
<a name="line542"></a> * the popup menu if the button is being hidden.
<a name="line543"></a> * @param {boolean} visible Whether to show or hide the button.
<a name="line544"></a> * @param {boolean=} opt_force If true, doesn&#39;t check whether the component
<a name="line545"></a> *     already has the requested visibility, and doesn&#39;t dispatch any events.
<a name="line546"></a> * @return {boolean} Whether the visibility was changed.
<a name="line547"></a> * @override
<a name="line548"></a> */
<a name="line549"></a>goog.ui.MenuButton.prototype.setVisible = function(visible, opt_force) {
<a name="line550"></a>  var visibilityChanged = goog.ui.MenuButton.superClass_.setVisible.call(this,
<a name="line551"></a>      visible, opt_force);
<a name="line552"></a>  if (visibilityChanged &amp;&amp; !this.isVisible()) {
<a name="line553"></a>    this.setOpen(false);
<a name="line554"></a>  }
<a name="line555"></a>  return visibilityChanged;
<a name="line556"></a>};
<a name="line557"></a>
<a name="line558"></a>
<a name="line559"></a>/**
<a name="line560"></a> * Enables/disables the menu button based on the value of the argument, and
<a name="line561"></a> * updates its CSS styling.  Also hides the popup menu if the button is being
<a name="line562"></a> * disabled.
<a name="line563"></a> * @param {boolean} enable Whether to enable or disable the button.
<a name="line564"></a> * @override
<a name="line565"></a> */
<a name="line566"></a>goog.ui.MenuButton.prototype.setEnabled = function(enable) {
<a name="line567"></a>  goog.ui.MenuButton.superClass_.setEnabled.call(this, enable);
<a name="line568"></a>  if (!this.isEnabled()) {
<a name="line569"></a>    this.setOpen(false);
<a name="line570"></a>  }
<a name="line571"></a>};
<a name="line572"></a>
<a name="line573"></a>
<a name="line574"></a>// TODO(nicksantos): AlignMenuToStart and ScrollOnOverflow and PositionElement
<a name="line575"></a>// should all be deprecated, in favor of people setting their own
<a name="line576"></a>// AnchoredPosition with the parameters they need. Right now, we try
<a name="line577"></a>// to be backwards-compatible as possible, but this is incomplete because
<a name="line578"></a>// the APIs are non-orthogonal.
<a name="line579"></a>
<a name="line580"></a>
<a name="line581"></a>/**
<a name="line582"></a> * @return {boolean} Whether the menu is aligned to the start of the button
<a name="line583"></a> *     (left if the render direction is left-to-right, right if the render
<a name="line584"></a> *     direction is right-to-left).
<a name="line585"></a> */
<a name="line586"></a>goog.ui.MenuButton.prototype.isAlignMenuToStart = function() {
<a name="line587"></a>  var corner = this.menuPosition_.corner;
<a name="line588"></a>  return corner == goog.positioning.Corner.BOTTOM_START ||
<a name="line589"></a>      corner == goog.positioning.Corner.TOP_START;
<a name="line590"></a>};
<a name="line591"></a>
<a name="line592"></a>
<a name="line593"></a>/**
<a name="line594"></a> * Sets whether the menu is aligned to the start or the end of the button.
<a name="line595"></a> * @param {boolean} alignToStart Whether the menu is to be aligned to the start
<a name="line596"></a> *     of the button (left if the render direction is left-to-right, right if
<a name="line597"></a> *     the render direction is right-to-left).
<a name="line598"></a> */
<a name="line599"></a>goog.ui.MenuButton.prototype.setAlignMenuToStart = function(alignToStart) {
<a name="line600"></a>  this.menuPosition_.corner = alignToStart ?
<a name="line601"></a>      goog.positioning.Corner.BOTTOM_START :
<a name="line602"></a>      goog.positioning.Corner.BOTTOM_END;
<a name="line603"></a>};
<a name="line604"></a>
<a name="line605"></a>
<a name="line606"></a>/**
<a name="line607"></a> * Sets whether the menu should scroll when it&#39;s too big to fix vertically on
<a name="line608"></a> * the screen.  The css of the menu element should have overflow set to auto.
<a name="line609"></a> * Note: Adding or removing items while the menu is open will not work correctly
<a name="line610"></a> * if scrollOnOverflow is on.
<a name="line611"></a> * @param {boolean} scrollOnOverflow Whether the menu should scroll when too big
<a name="line612"></a> *     to fit on the screen.  If false, adjust logic will be used to try and
<a name="line613"></a> *     reposition the menu to fit.
<a name="line614"></a> */
<a name="line615"></a>goog.ui.MenuButton.prototype.setScrollOnOverflow = function(scrollOnOverflow) {
<a name="line616"></a>  if (this.menuPosition_.setLastResortOverflow) {
<a name="line617"></a>    var overflowX = goog.positioning.Overflow.ADJUST_X;
<a name="line618"></a>    var overflowY = scrollOnOverflow ?
<a name="line619"></a>        goog.positioning.Overflow.RESIZE_HEIGHT :
<a name="line620"></a>        goog.positioning.Overflow.ADJUST_Y;
<a name="line621"></a>    this.menuPosition_.setLastResortOverflow(overflowX | overflowY);
<a name="line622"></a>  }
<a name="line623"></a>};
<a name="line624"></a>
<a name="line625"></a>
<a name="line626"></a>/**
<a name="line627"></a> * @return {boolean} Wether the menu will scroll when it&#39;s to big to fit
<a name="line628"></a> *     vertically on the screen.
<a name="line629"></a> */
<a name="line630"></a>goog.ui.MenuButton.prototype.isScrollOnOverflow = function() {
<a name="line631"></a>  return this.menuPosition_.getLastResortOverflow &amp;&amp;
<a name="line632"></a>      !!(this.menuPosition_.getLastResortOverflow() &amp;
<a name="line633"></a>         goog.positioning.Overflow.RESIZE_HEIGHT);
<a name="line634"></a>};
<a name="line635"></a>
<a name="line636"></a>
<a name="line637"></a>/**
<a name="line638"></a> * @return {boolean} Whether the attached menu is focusable.
<a name="line639"></a> */
<a name="line640"></a>goog.ui.MenuButton.prototype.isFocusablePopupMenu = function() {
<a name="line641"></a>  return this.isFocusablePopupMenu_;
<a name="line642"></a>};
<a name="line643"></a>
<a name="line644"></a>
<a name="line645"></a>/**
<a name="line646"></a> * Sets whether the attached popup menu is focusable.  If the popup menu is
<a name="line647"></a> * focusable, it may steal keyboard focus from the menu button, so the button
<a name="line648"></a> * will not hide the menu on blur.
<a name="line649"></a> * @param {boolean} focusable Whether the attached menu is focusable.
<a name="line650"></a> */
<a name="line651"></a>goog.ui.MenuButton.prototype.setFocusablePopupMenu = function(focusable) {
<a name="line652"></a>  // TODO(attila):  The menu itself should advertise whether it is focusable.
<a name="line653"></a>  this.isFocusablePopupMenu_ = focusable;
<a name="line654"></a>};
<a name="line655"></a>
<a name="line656"></a>
<a name="line657"></a>/**
<a name="line658"></a> * Sets whether to render the menu as a sibling element of the button.
<a name="line659"></a> * Normally, the menu is a child of document.body.  This option is useful if
<a name="line660"></a> * you need the menu to inherit styles from a common parent element, or if you
<a name="line661"></a> * otherwise need it to share a parent element for desired event handling.  One
<a name="line662"></a> * example of the latter is if the parent is in a goog.ui.Popup, to ensure that
<a name="line663"></a> * clicks on the menu are considered being within the popup.
<a name="line664"></a> * @param {boolean} renderMenuAsSibling Whether we render the menu at the end
<a name="line665"></a> *     of the dom or as a sibling to the button/label that renders the drop
<a name="line666"></a> *     down.
<a name="line667"></a> */
<a name="line668"></a>goog.ui.MenuButton.prototype.setRenderMenuAsSibling = function(
<a name="line669"></a>    renderMenuAsSibling) {
<a name="line670"></a>  this.renderMenuAsSibling_ = renderMenuAsSibling;
<a name="line671"></a>};
<a name="line672"></a>
<a name="line673"></a>
<a name="line674"></a>/**
<a name="line675"></a> * Reveals the menu and hooks up menu-specific event handling.
<a name="line676"></a> * @deprecated Use {@link #setOpen} instead.
<a name="line677"></a> */
<a name="line678"></a>goog.ui.MenuButton.prototype.showMenu = function() {
<a name="line679"></a>  this.setOpen(true);
<a name="line680"></a>};
<a name="line681"></a>
<a name="line682"></a>
<a name="line683"></a>/**
<a name="line684"></a> * Hides the menu and cleans up menu-specific event handling.
<a name="line685"></a> * @deprecated Use {@link #setOpen} instead.
<a name="line686"></a> */
<a name="line687"></a>goog.ui.MenuButton.prototype.hideMenu = function() {
<a name="line688"></a>  this.setOpen(false);
<a name="line689"></a>};
<a name="line690"></a>
<a name="line691"></a>
<a name="line692"></a>/**
<a name="line693"></a> * Opens or closes the attached popup menu.
<a name="line694"></a> * @param {boolean} open Whether to open or close the menu.
<a name="line695"></a> * @param {goog.events.Event=} opt_e Mousedown event that caused the menu to
<a name="line696"></a> *     be opened.
<a name="line697"></a> * @override
<a name="line698"></a> */
<a name="line699"></a>goog.ui.MenuButton.prototype.setOpen = function(open, opt_e) {
<a name="line700"></a>  goog.ui.MenuButton.superClass_.setOpen.call(this, open);
<a name="line701"></a>  if (this.menu_ &amp;&amp; this.hasState(goog.ui.Component.State.OPENED) == open) {
<a name="line702"></a>    if (open) {
<a name="line703"></a>      if (!this.menu_.isInDocument()) {
<a name="line704"></a>        if (this.renderMenuAsSibling_) {
<a name="line705"></a>          this.menu_.render(/** @type {Element} */ (
<a name="line706"></a>              this.getElement().parentNode));
<a name="line707"></a>        } else {
<a name="line708"></a>          this.menu_.render();
<a name="line709"></a>        }
<a name="line710"></a>      }
<a name="line711"></a>      this.viewportBox_ =
<a name="line712"></a>          goog.style.getVisibleRectForElement(this.getElement());
<a name="line713"></a>      this.buttonRect_ = goog.style.getBounds(this.getElement());
<a name="line714"></a>      this.positionMenu();
<a name="line715"></a>      this.menu_.setHighlightedIndex(-1);
<a name="line716"></a>    } else {
<a name="line717"></a>      this.setActive(false);
<a name="line718"></a>      this.menu_.setMouseButtonPressed(false);
<a name="line719"></a>
<a name="line720"></a>      var element = this.getElement();
<a name="line721"></a>      // Clear any remaining a11y state.
<a name="line722"></a>      if (element) {
<a name="line723"></a>        goog.a11y.aria.setState(element,
<a name="line724"></a>            goog.a11y.aria.State.ACTIVEDESCENDANT,
<a name="line725"></a>            &#39;&#39;);
<a name="line726"></a>      }
<a name="line727"></a>
<a name="line728"></a>      // Clear any sizes that might have been stored.
<a name="line729"></a>      if (goog.isDefAndNotNull(this.originalSize_)) {
<a name="line730"></a>        this.originalSize_ = undefined;
<a name="line731"></a>        var elem = this.menu_.getElement();
<a name="line732"></a>        if (elem) {
<a name="line733"></a>          goog.style.setSize(elem, &#39;&#39;, &#39;&#39;);
<a name="line734"></a>        }
<a name="line735"></a>      }
<a name="line736"></a>    }
<a name="line737"></a>    this.menu_.setVisible(open, false, opt_e);
<a name="line738"></a>    // In Pivot Tables the menu button somehow gets disposed of during the
<a name="line739"></a>    // setVisible call, causing attachPopupListeners_ to fail.
<a name="line740"></a>    // TODO(user): Debug what happens.
<a name="line741"></a>    if (!this.isDisposed()) {
<a name="line742"></a>      this.attachPopupListeners_(open);
<a name="line743"></a>    }
<a name="line744"></a>  }
<a name="line745"></a>};
<a name="line746"></a>
<a name="line747"></a>
<a name="line748"></a>/**
<a name="line749"></a> * Resets the MenuButton&#39;s size.  This is useful for cases where items are added
<a name="line750"></a> * or removed from the menu and scrollOnOverflow is on.  In those cases the
<a name="line751"></a> * menu will not behave correctly and resize itself unless this is called
<a name="line752"></a> * (usually followed by positionMenu()).
<a name="line753"></a> */
<a name="line754"></a>goog.ui.MenuButton.prototype.invalidateMenuSize = function() {
<a name="line755"></a>  this.originalSize_ = undefined;
<a name="line756"></a>};
<a name="line757"></a>
<a name="line758"></a>
<a name="line759"></a>/**
<a name="line760"></a> * Positions the menu under the button.  May be called directly in cases when
<a name="line761"></a> * the menu size is known to change.
<a name="line762"></a> */
<a name="line763"></a>goog.ui.MenuButton.prototype.positionMenu = function() {
<a name="line764"></a>  if (!this.menu_.isInDocument()) {
<a name="line765"></a>    return;
<a name="line766"></a>  }
<a name="line767"></a>
<a name="line768"></a>  var positionElement = this.positionElement_ || this.getElement();
<a name="line769"></a>  var position = this.menuPosition_;
<a name="line770"></a>  this.menuPosition_.element = positionElement;
<a name="line771"></a>
<a name="line772"></a>  var elem = this.menu_.getElement();
<a name="line773"></a>  if (!this.menu_.isVisible()) {
<a name="line774"></a>    elem.style.visibility = &#39;hidden&#39;;
<a name="line775"></a>    goog.style.showElement(elem, true);
<a name="line776"></a>  }
<a name="line777"></a>
<a name="line778"></a>  if (!this.originalSize_ &amp;&amp; this.isScrollOnOverflow()) {
<a name="line779"></a>    this.originalSize_ = goog.style.getSize(elem);
<a name="line780"></a>  }
<a name="line781"></a>  var popupCorner = goog.positioning.flipCornerVertical(position.corner);
<a name="line782"></a>  position.reposition(elem, popupCorner, this.menuMargin_, this.originalSize_);
<a name="line783"></a>
<a name="line784"></a>  if (!this.menu_.isVisible()) {
<a name="line785"></a>    goog.style.showElement(elem, false);
<a name="line786"></a>    elem.style.visibility = &#39;visible&#39;;
<a name="line787"></a>  }
<a name="line788"></a>};
<a name="line789"></a>
<a name="line790"></a>
<a name="line791"></a>/**
<a name="line792"></a> * Periodically repositions the menu while it is visible.
<a name="line793"></a> *
<a name="line794"></a> * @param {goog.events.Event} e An event object.
<a name="line795"></a> * @private
<a name="line796"></a> */
<a name="line797"></a>goog.ui.MenuButton.prototype.onTick_ = function(e) {
<a name="line798"></a>  // Call positionMenu() only if the button position or size was
<a name="line799"></a>  // changed, or if the window&#39;s viewport was changed.
<a name="line800"></a>  var currentButtonRect = goog.style.getBounds(this.getElement());
<a name="line801"></a>  var currentViewport = goog.style.getVisibleRectForElement(this.getElement());
<a name="line802"></a>  if (!goog.math.Rect.equals(this.buttonRect_, currentButtonRect) ||
<a name="line803"></a>      !goog.math.Box.equals(this.viewportBox_, currentViewport)) {
<a name="line804"></a>    this.buttonRect_ = currentButtonRect;
<a name="line805"></a>    this.viewportBox_ = currentViewport;
<a name="line806"></a>    this.positionMenu();
<a name="line807"></a>  }
<a name="line808"></a>};
<a name="line809"></a>
<a name="line810"></a>
<a name="line811"></a>/**
<a name="line812"></a> * Attaches or detaches menu event listeners to/from the given menu.
<a name="line813"></a> * Called each time a menu is attached to or detached from the button.
<a name="line814"></a> * @param {goog.ui.Menu} menu Menu on which to listen for events.
<a name="line815"></a> * @param {boolean} attach Whether to attach or detach event listeners.
<a name="line816"></a> * @private
<a name="line817"></a> */
<a name="line818"></a>goog.ui.MenuButton.prototype.attachMenuEventListeners_ = function(menu,
<a name="line819"></a>    attach) {
<a name="line820"></a>  var handler = this.getHandler();
<a name="line821"></a>  var method = attach ? handler.listen : handler.unlisten;
<a name="line822"></a>
<a name="line823"></a>  // Handle events dispatched by menu items.
<a name="line824"></a>  method.call(handler, menu, goog.ui.Component.EventType.ACTION,
<a name="line825"></a>      this.handleMenuAction);
<a name="line826"></a>  method.call(handler, menu, goog.ui.Component.EventType.HIGHLIGHT,
<a name="line827"></a>      this.handleHighlightItem);
<a name="line828"></a>  method.call(handler, menu, goog.ui.Component.EventType.UNHIGHLIGHT,
<a name="line829"></a>      this.handleUnHighlightItem);
<a name="line830"></a>};
<a name="line831"></a>
<a name="line832"></a>
<a name="line833"></a>/**
<a name="line834"></a> * Handles {@code HIGHLIGHT} events dispatched by the attached menu.
<a name="line835"></a> * @param {goog.events.Event} e Highlight event to handle.
<a name="line836"></a> */
<a name="line837"></a>goog.ui.MenuButton.prototype.handleHighlightItem = function(e) {
<a name="line838"></a>  var element = this.getElement();
<a name="line839"></a>  goog.asserts.assert(element, &#39;The menu button DOM element cannot be null.&#39;);
<a name="line840"></a>  goog.a11y.aria.setState(element,
<a name="line841"></a>      goog.a11y.aria.State.ACTIVEDESCENDANT,
<a name="line842"></a>      e.target.getElement().id);
<a name="line843"></a>};
<a name="line844"></a>
<a name="line845"></a>
<a name="line846"></a>/**
<a name="line847"></a> * Handles UNHIGHLIGHT events dispatched by the associated menu.
<a name="line848"></a> * @param {goog.events.Event} e Unhighlight event to handle.
<a name="line849"></a> */
<a name="line850"></a>goog.ui.MenuButton.prototype.handleUnHighlightItem = function(e) {
<a name="line851"></a>  if (!this.menu_.getHighlighted()) {
<a name="line852"></a>    var element = this.getElement();
<a name="line853"></a>    goog.asserts.assert(element, &#39;The menu button DOM element cannot be null.&#39;);
<a name="line854"></a>    goog.a11y.aria.setState(element,
<a name="line855"></a>        goog.a11y.aria.State.ACTIVEDESCENDANT,
<a name="line856"></a>        &#39;&#39;);
<a name="line857"></a>  }
<a name="line858"></a>};
<a name="line859"></a>
<a name="line860"></a>
<a name="line861"></a>/**
<a name="line862"></a> * Attaches or detaches event listeners depending on whether the popup menu
<a name="line863"></a> * is being shown or hidden.  Starts listening for document mousedown events
<a name="line864"></a> * and for menu blur events when the menu is shown, and stops listening for
<a name="line865"></a> * these events when it is hidden.  Called from {@link #setOpen}.
<a name="line866"></a> * @param {boolean} attach Whether to attach or detach event listeners.
<a name="line867"></a> * @private
<a name="line868"></a> */
<a name="line869"></a>goog.ui.MenuButton.prototype.attachPopupListeners_ = function(attach) {
<a name="line870"></a>  var handler = this.getHandler();
<a name="line871"></a>  var method = attach ? handler.listen : handler.unlisten;
<a name="line872"></a>
<a name="line873"></a>  // Listen for document mousedown events in the capture phase, because
<a name="line874"></a>  // the target may stop propagation of the event in the bubble phase.
<a name="line875"></a>  method.call(handler, this.getDomHelper().getDocument(),
<a name="line876"></a>      goog.events.EventType.MOUSEDOWN, this.handleDocumentMouseDown, true);
<a name="line877"></a>
<a name="line878"></a>  // Only listen for blur events dispatched by the menu if it is focusable.
<a name="line879"></a>  if (this.isFocusablePopupMenu()) {
<a name="line880"></a>    method.call(handler, /** @type {goog.events.EventTarget} */ (this.menu_),
<a name="line881"></a>        goog.ui.Component.EventType.BLUR, this.handleMenuBlur);
<a name="line882"></a>  }
<a name="line883"></a>
<a name="line884"></a>  method.call(handler, this.timer_, goog.Timer.TICK, this.onTick_);
<a name="line885"></a>  if (attach) {
<a name="line886"></a>    this.timer_.start();
<a name="line887"></a>  } else {
<a name="line888"></a>    this.timer_.stop();
<a name="line889"></a>  }
<a name="line890"></a>};
<a name="line891"></a>
<a name="line892"></a>
<a name="line893"></a>// Register a decorator factory function for goog.ui.MenuButtons.
<a name="line894"></a>goog.ui.registry.setDecoratorByClassName(goog.ui.MenuButtonRenderer.CSS_CLASS,
<a name="line895"></a>    function() {
<a name="line896"></a>      // MenuButton defaults to using MenuButtonRenderer.
<a name="line897"></a>      return new goog.ui.MenuButton(null);
<a name="line898"></a>    });
</pre>


</body>
</html>
